package com.douxg.fastCoding.db.mysql;

import com.douxg.fastCoding.db.DbDataType;
import com.douxg.fastCoding.db.JavaDbDataType;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.ibatis.type.JdbcType;

/**
 * @Description
 * @Auther douxg
 * @Date 2020/6/15 20:47
 */
@Getter
@AllArgsConstructor
public enum MysqlDbDataType implements DbDataType {
    BIT(JdbcType.BIT, JavaDbDataType.BOOLEAN),
    TINYINT(JdbcType.TINYINT, JavaDbDataType.INTEGER), SMALLINT(JdbcType.SMALLINT, JavaDbDataType.INTEGER),
    MEDIUMINT(JdbcType.INTEGER, JavaDbDataType.INTEGER), INT(JdbcType.INTEGER, JavaDbDataType.INTEGER),
    INTEGER(JdbcType.INTEGER, JavaDbDataType.INTEGER), BIGINT(JdbcType.BIGINT, JavaDbDataType.LONG),
    FLOAT(JdbcType.FLOAT, JavaDbDataType.DOUBLE), DOUBLE(JdbcType.DOUBLE, JavaDbDataType.DOUBLE),
    DECIMAL(JdbcType.DECIMAL, JavaDbDataType.BIG_DECIMAL), DATE(JdbcType.DATE, JavaDbDataType.LOCAL_DATE),
    TIME(JdbcType.TIME, JavaDbDataType.LOCAL_TIME), YEAR(JdbcType.DATE, JavaDbDataType.LOCAL_DATE),
    DATETIME(JdbcType.TIMESTAMP, JavaDbDataType.LOCAL_DATE_TIME), TIMESTAMP(JdbcType.TIMESTAMP, JavaDbDataType.LOCAL_DATE_TIME),
    CHAR(JdbcType.CHAR, JavaDbDataType.STRING), VARCHAR(JdbcType.VARCHAR, JavaDbDataType.STRING),
    TINYBLOB(JdbcType.BLOB, JavaDbDataType.STRING), TINYTEXT(JdbcType.LONGVARCHAR, JavaDbDataType.STRING),
    BLOB(JdbcType.BLOB, JavaDbDataType.STRING), TEXT(JdbcType.LONGVARCHAR, JavaDbDataType.STRING),
    MEDIUMBLOB(JdbcType.BLOB, JavaDbDataType.STRING), MEDIUMTEXT(JdbcType.LONGVARCHAR, JavaDbDataType.STRING),
    LONGBLOB(JdbcType.BLOB, JavaDbDataType.STRING), LONGTEXT(JdbcType.LONGVARCHAR, JavaDbDataType.STRING);
    private JdbcType mybatisJdbcType;
    private JavaDbDataType javaDbDataType;

    public static MysqlDbDataType getByName(String name) {
        if (name != null) {
            for (MysqlDbDataType value : values()) {
                if (value.name().toLowerCase().equals(name) || value.name().equals(name)) {
                    return value;
                }
            }
        }
        return null;
    }

    @Override
    public boolean isDateTime() {
        if (this == DATE || this == TIME || this == YEAR || this == DATETIME || this == TIMESTAMP) {
            return true;
        }
        return false;
    }
}
